import json
import re
import requests

url = 'https://yys.pkma.cc/api/yyscbg/search_free'
a = {
    "page":1,
          "page_size":10,
          "filter":
              {
                  "server_name":["网易-两情相悦"],
               "status_des":"2",
               "all_hero_fragments":[{
                   "name":"桔梗",
                   "num":10}]},
          "sort":{
              "order_by":"price",
              "ascending":True
          }
}
b = {
  "page": 1,
  "page_size": 20,
  "filter": {
    "status_des": "2",
    "all_hero_fragments": [
      {
        "name": "桔梗",
        "num": 10
      }
    ]
  },
  "sort": {
    "order_by": "price",
    "ascending": True
  }
}
def http(url,params):

    result = requests.post(url,json = params)
    # 返回链接的json字符串
    return result

def find_key(d, target_key):
    """
    递归查找字典中的目标键并返回其值。
    :param d: 要搜索的字典
    :param target_key: 目标键
    :return: 目标键的值列表
    """
    values = []
    if isinstance(d, dict):
        for key, value in d.items():
            if key == target_key:
                values.append(value)
            elif isinstance(value, (dict, list)):
                values.extend(find_key(value, target_key))
    elif isinstance(d, list):
        for item in d:
            values.extend(find_key(item, target_key))
    return values


# 将 JSON 字符串转换为 Python 对象
jsons = http(url,a).text
# jsons = http(url,b).text
data = json.loads(jsons)
# print(jsons)
def name_value(target_key):

    result = find_key(data, target_key)

    return result
price = name_value('price')
ssr_hava_num = name_value('ssr_hava_num')
sp_hava_num = name_value('sp_hava_num')
game_ordersn = name_value('game_ordersn')
allow_bargain = name_value('allow_bargain')
pattern = r'-\d+-'

# 提取数字
extracted_numbers = []
for s in game_ordersn:
    match = re.search(pattern, s)
    if match:
        # 去掉 - 符号，只保留数字
        number = match.group().strip('-')
        extracted_numbers.append(number)


price_allow = []
for i in range(len(allow_bargain)):
    if allow_bargain[i] == 1:
        price_allow.append('可')
    else:
        price_allow.append('不')

for price, game_ordersn,price_allow,ssr_hava_num,sp_hava_num,extracted_numbers in zip(price, game_ordersn,price_allow,ssr_hava_num,sp_hava_num,extracted_numbers):


    print(f"{price}{price_allow},{ssr_hava_num},{sp_hava_num} ,https://yys.cbg.163.com/cgi/mweb/equip/{extracted_numbers}/{game_ordersn}")